#include "log.h"
#include "dirty.h"
#include "listener.h"
#include "poll_thread.h"
#include "daemon.h"
#include "config.h"

#include <unistd.h>
#include <iostream>
#include <fstream>
using namespace std;

#define SHM_KEY 0x00001111

char confFile[256] = "filterd.conf";
char dirtyFile[256];

const char progname[] = "filterd";
const char usage_argv[] = "";
const char version[] = "1.0";

volatile int stop = 0;
int background = 1;
void initMain(CPollerUnit* unit);
CConfig g_config;

int main(int argc,char** argv)
{

	
	if(DaemonInit(argc, argv) < 0)
	{
		return -1;
	}
	
	if (DaemonStart() < 0)
	{
		return -1;
	}


	cout << "parse config" << endl;
	
	if(g_config.ParseConfig(confFile) == -1)
	{
		log_error("parse config %s failed",confFile);
		return -1;
	}

	set_log_level(g_config.GetIntVal("LOG", "level"));

	
	Dirty* pDirty = Dirty::getInstance();
	if(!pDirty->init(g_config.GetIntVal("SHAREMEM","key")))
	{
		log_error("dirty init failed:%d",SHM_KEY);
		return -1;
	}
	
	CListener listener(g_config.GetStrVal("SERVER","ip"),g_config.GetIntVal("SERVER","port"));
	
	CPollThread pThread("filterd",1024);
	pThread.InitializeThread(0);
	
	if(listener.Attach(&pThread,1024) == -1)
		return -1;
	
	pThread.RunningThread();

	
	return 0;
}


